﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using StockManagement.Commands;
using StockManagement.Helper;

namespace StockManagement.ViewModels
{
    public class InwardProductOUTViewModel:BaseViewModel
    {
        ObservableCollection<ProductViewModel> _InwardOUTProductCollection = new ObservableCollection<ProductViewModel>();        
        ProductViewModel _productVM;
        DelegateCommand _updateCommand;
        public event ProductUpdatedDelegate OnInwardProductOUTUpdated;

        public InwardProductOUTViewModel(ObservableCollection<ProductViewModel> allProductCollection)
        {
            PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(InwardProductViewModel_PropertyChanged);
            
            foreach (var item in allProductCollection)
            {
                if (item.ProductType == Helper.EnumMaterialType.Manufacturing && item.ProductStatus == Helper.ProductStatus.Inward_InProgress)
                    _InwardOUTProductCollection.Add(item);
            }
            InwardOUTProductCollection.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(InwardOUTProductCollection_CollectionChanged);
            _updateCommand = new DelegateCommand(ExecuteUpdateCommand, CanExecuteCommand);
            SelectedProduct = _InwardOUTProductCollection.FirstOrDefault();
        }

        void InwardProductViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            //switch (e.PropertyName)
            //{
            //    case "SelectedProduct":
            //        InitializeProperties();
            //        //if (SelectedProduct == null)
            //        //    _productstatus = string.Empty;
            //        //else
            //        //    _productstatus = Helper.ProductStatus.Inward_Completed.ToString();
            //        break;
            //    default:
            //        break;
            //}
            UpdateCommand.RaiseCanExecuteChanged();
        }

        void InwardOUTProductCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (SelectedProduct == null && InwardOUTProductCollection.Count > 0)
                SelectedProduct = InwardOUTProductCollection.FirstOrDefault();
        }

        public ProductViewModel SelectedProduct
        {
            get
            {
                return _productVM;
            }
            set
            {
                _productVM = value;
                OnPropertyChanged("SelectedProduct");
            }
        }

        public ObservableCollection<ProductViewModel> InwardOUTProductCollection
        {
            get
            {
                return _InwardOUTProductCollection;
            }
        }

        public DelegateCommand UpdateCommand
        {
            get
            {
                return _updateCommand;
            }
        }

        private bool CanExecuteCommand(object parameter)
        {
            return (SelectedProduct != null);
        }

        private void ExecuteUpdateCommand(object parameter)
        {
            string strmessage = string.Format("Do you want to mark the product '{0}' as Completed?", SelectedProduct.ProductName);
            var result = ShowConfirmationMessageBox(strmessage, "Product Completed");
            if (!result)
                return;

            if (!_productVM.UpdateProduct(UpdateType.ConsumeComplete))
                return;

            if ((OnInwardProductOUTUpdated != null) &&
                OnInwardProductOUTUpdated(_productVM))
                InwardOUTProductCollection.Remove(_productVM);
        }

        public void UpdateProductProperties(ProductViewModel product)
        {
            product.ProductStatus = Helper.ProductStatus.Outward_ToBeDeliverd;
        }
    }
}
